package na.learn.java.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Array;

public class Pattern {
	
	
	final static char[] digits = {
		'0' , '1' , '2' , '3' , '4' , '5' ,
		'6' , '7' , '8' , '9' , 'a' , 'b' ,
		'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
		'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
		'o' , 'p' , 'q' , 'r' , 's' , 't' ,
		'u' , 'v' , 'w' , 'x' , 'y' , 'z'
	    };

	

	private static String padIt(StringBuffer result, int len) {
		StringBuffer tmpBuffer = new StringBuffer();
		for (int paddingLen = len - result.length(); paddingLen > 0; paddingLen--)
			tmpBuffer.append("0");
		tmpBuffer.append(result.toString());
		return tmpBuffer.toString();
	}

	public static String[] parse(String sourceString) {
		String tmp = null;
		String[] outputArray = null;

		char[] chArr = sourceString.toCharArray();
		char[] newCharArr = new char[chArr.length];
		int counter = 0, numSpaces = 0;
		try {
			for (char ch : chArr) {
				if (numSpaces++ >= 0 && ch == ' ') {
					newCharArr[counter++] = '\n';
					;
				} else
					newCharArr[counter++] = ch;
			}

			BufferedReader buffReader = new BufferedReader(new StringReader(new String(newCharArr)));

			outputArray = (String[]) Array.newInstance(String.class, numSpaces);
			StringBuffer strBuffer = new StringBuffer();
			String str;
			int count = 0;
			while ((str = buffReader.readLine()) != null)
				outputArray[count++] = str;

		} catch (IOException e) {
			e.printStackTrace();
		}
		return outputArray;
	}

	public static boolean hasNOnes(long number, int N) {
		byte pos = 1;

		while (number != 0) {
			if ((number & 1) == 1)
				pos <<= 1;
			number >>= 1;
		}

		return pos == Math.pow(2, N);
	}

	public static void main(String[] args) {

		//	parse("This is a sentence");

		/*for (long count = 1; count < 40l; count++)
		 System.out.println(getBitPattern(count));*/

		//for (long count = 1; count < 40l; count++)
		testTwnis(15l);

	}
	
	
	 public static String toUnsignedString(int i, int shift) {
			char[] buf = new char[32];
			int charPos = 32;
			int radix = 1 << shift;
			int mask = radix - 1;
			do {
			    buf[--charPos] = digits[i & mask];
			    i >>>= shift;
			} while (i != 0);

			return new String(buf, charPos, (32 - charPos));
		    }

	private static void testTwnis(long range) {

		for (int count = 0; count < range; count++) {
			if (hasNOnes(count, 3))
				System.out.println("count: " + count + " :  " + toUnsignedString(count,1));
		}

	}

}
